package com.example.zhangyang.andriodfirstapp;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class DisplayMessageActivity extends AppCompatActivity {

    private double aDouble;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_display_message);

        //创建Intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        //获得第二个界面的TextView
        TextView textView = findViewById(R.id.textViewZY);
        textView.setText(message);

        //纯代码布局
        initView();
    }

    public void initView () {

        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);


        for (int i = 0; i < 5; i++) {
            //首先设置一个全局的系统尺寸
            LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            //五个textView建立五个LinearLayout
            LinearLayout lLayout = new LinearLayout(this);
            lLayout.setId(i + 10);
            lLayout.setOrientation(LinearLayout.HORIZONTAL);
            lLayout.setLayoutParams(llParams);

            TextView tv = new TextView(this);
            tv.setId(i);
            tv.setText("这个是第" + i + "个文本框");
            tv.setTextColor(Color.RED);

            TextView tv_num = new TextView(this);
            tv_num.setId(i + 20);
            tv_num.setText(i + "台");
            tv_num.setTextColor(Color.BLUE);

            //给tv和tv_num添加长高
            ViewGroup.LayoutParams layoutParams_txt = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            tv.setLayoutParams(layoutParams_txt);
            tv_num.setLayoutParams(layoutParams_txt);

            //添加到每行的LinearLayout中
            lLayout.addView(tv);
            lLayout.addView(tv_num);

            // 每个linearlayout都在前一个的下面，第一个在顶,不处理

            // 把每个linearlayout加到relativelayout中
            layout.addView(lLayout);
        }

        Button button = new Button(this);
        ViewGroup.LayoutParams btnParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        button.setText(R.string.button_custom);
        button.setLayoutParams(btnParams);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ConverView();
            }
        });

        layout.addView(button);

        setContentView(layout);
    }

    public void ConverView () {
        final LinearLayout mask = new LinearLayout(this);
        mask.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams maskParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mask.setBackgroundColor(getResources().getColor(R.color.red));
        aDouble = 0.5;
        mask.setAlpha((float) aDouble);

        Button btn = new Button(this);
        final Button btn2 = new Button(this);
        ViewGroup.LayoutParams btnParams = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        btn.setText("click");
        btn2.setText("click2");
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //mask.removeView(btn2);
                //mask.removeAllViews();
                //mask.setVisibility(View.GONE);
                //一层一层覆盖View
                ConverView();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //一层一层的剥离View
                mask.setVisibility(View.GONE);
            }
        });
        mask.addView(btn,btnParams);


        //addView是在对应的布局上增加View
        mask.addView(btn2,btnParams);


        //是在原有的视图上覆盖一个视图(就是这里的截图2上的)
        addContentView(mask,maskParams);

    }


    public void button2ZYClick (View view) {
        Toast.makeText(this, "Button2ZYClick", Toast.LENGTH_SHORT).show();
    }

}
